/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.region.mapper;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import net.siufung.market.base.region.controller.model.dto.RegionTreeDto;
import net.siufung.market.base.region.mapper.entity.Region;
import net.siufung.mybatis.base.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 行政区域信息 Mapper 接口
 * </p>
 *
 * @author 陈建峰
 * @since 2022-07-07 14:18:39
 */
@Mapper
public interface IRegionMapper extends BaseMapper<Region> {

    /**
     * 获取区域树
     * @param parentId 父级Id
     * @return List<RegionTreeNode>
     */
    default List<Tree<Integer>> getRegionTree(Integer parentId) {
        // Region parentRegion = this.selectById(parentId);
        List<Region> childRegionList = this.selectList(
                new LambdaQueryWrapper<Region>().eq(Region::getParentId, parentId));
        List<Integer> childRegionIdList =
                childRegionList.stream().map(Region::getId).collect(Collectors.toList());
        List<Region> regionList = this.selectList(
                new LambdaQueryWrapper<Region>().in(Region::getParentId, childRegionIdList));
        // regionList.add(parentRegion);
        regionList.addAll(childRegionList);

        return TreeUtil.build(
                regionList, parentId, new TreeNodeConfig(), (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getSequence());
                    tree.setName(treeNode.getName());
                    tree.putExtra("code", treeNode.getCode());
                    tree.putExtra("lng", treeNode.getLng());
                    tree.putExtra("lat", treeNode.getLat());
                    tree.putExtra("level", treeNode.getLevel());
                });
    }

    /**
     * 获得区域树
     * @param regionTreeDto 区域树
     * @return List<Tree<Integer>>
     */
    default List<Tree<Integer>> getTreeTable(RegionTreeDto regionTreeDto){
        List<Region> regionList = this.selectList(new LambdaQueryWrapper<Region>()
                .like(!Strings.isNullOrEmpty(regionTreeDto.getCode()), Region::getCode, regionTreeDto.getCode())
                .like(!Strings.isNullOrEmpty(regionTreeDto.getName()), Region::getName, regionTreeDto.getName())
                .eq(Objects.nonNull(regionTreeDto.getProvinceId()), Region::getParentId, regionTreeDto.getProvinceId())
                .eq(Objects.nonNull(regionTreeDto.getCityId()), Region::getParentId, regionTreeDto.getCityId())
                .eq(Objects.nonNull(regionTreeDto.getAreaId()), Region::getParentId, regionTreeDto.getAreaId()));
        if(CollectionUtils.isEmpty(regionList)){
            return Lists.newArrayList();
        }
        return TreeUtil.build(
                regionList, 0, new TreeNodeConfig(), (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getSequence());
                    tree.setName(treeNode.getName());
                    tree.putExtra("code", treeNode.getCode());
                    tree.putExtra("lng", treeNode.getLng());
                    tree.putExtra("lat", treeNode.getLat());
                    tree.putExtra("level", treeNode.getLevel());
                });
    }

}
